<?php $this->jQuery()->enable()?>
<?php $this->JQuery()->javascriptCaptureStart()?>
jQuery(function() {
	
	$j('.gallery .thumb').click(function(){
		//$('#main_photo1').attr('src', $(this).attr('rel2'));		
		$j('.img_cont').html("<img class='main_photo1' src='" + $(this).attr('rel2') + "'>");
		$('#main_photo1').attr('src', $(this).attr('rel2'));
	});
	
	if($j('.photo_act').css('display')=='hidden')
	{
		$j('#container').attr('class', 'gallery ad_detail');    
		$j('.info_act').hide();
		$j('.general').hide();
		$j('.category').hide();
		$j('.description').hide();
		$j('.gallery2').hide();
		$j('.photo_act').show();
		$j('.gallery1').show();
	}
	else
	{
		$j('#container').attr('class', 'info ad_detail'); 
		$j('.photo_act').hide();
		$j('.gallery1').hide();
		$j('.info_act').show();
		$j('.general').show();
		$j('.category').show();
		$j('.description').show();
		$j('.gallery2').show();
	}
	
	$j('.photo_tab').click(function(){
		$j('#container').attr('class', 'gallery ad_detail');  
		$j('.info_act').hide();;
		$j('.category').hide();
		$j('.description').hide();
		$j('.gallery2').hide();
		$j('.general').hide();
		$j('.photo_act').show();
		$j('.gallery1').show();		
	});
	
	$j('.gallery2 a').click(function(){
		$j('#container').attr('class', 'gallery ad_detail');  
		$j('.info_act').hide();
		$j('.category').hide();
		$j('.description').hide();
		$j('.gallery2').hide();
		$j('.general').hide();
		$j('.photo_act').show();
		$j('.gallery1').show();		
	});
		
	$j('.info_tab').click(function(){
		$j('#container').attr('class', 'info ad_detail');  
		$j('.photo_act').hide();
		$j('.gallery1').hide();
		$j('.info_act').show();
		$j('.general').show();
		$j('.category').show();
		$j('.description').show();		
		$j('.gallery2').show();
		$j('.gallery2 a').show();
	});
	
	$j('textarea#newmessage-message').keydown(function(event) {
        var charLength = 3000 - $j(this).val().length;
        if (charLength <0) {
            if ( event.keyCode == 8 || event.keyCode == 46 ) {

            } else {
                return false;
            }
        }
        else {
            $j('span#text_counter').html('<em>' + charLength + '</em> <?php echo $this->t('characters remaining')?>');
        }
    });
    
    if($j('.errors').css('display')=='block')
	{
		//$j('#newmessage-message').focus();
		//alert($(this).parent().attr('class'));
		//$j('.errors').focus();
	}

    
    $j(".to_bookmark2").click
	(
		function()
		{
			var a=$j(this);
			if(a.hasClass('save'))
			{
				jQuery.post
				(
					"/default/index/bookmark",
					{data:$j(this).attr("rel")},
					function(t)
					{
						if(t.res=="redirect")
						{
							window.location = '/user/login';
						}
						else
						{	
							
							if(a.hasClass('save'))
							{
								//window.location = '/user/saved';
								a.removeClass('save').addClass('saved').html('<span><?php echo $this->t('Remove Saved Ad')?></span>');
							}	
							else
							{
								a.removeClass('saved').addClass('save').html('<span><?php echo $this->t('Save This Ad')?></span>');		
							}
								
						}
					},
					"json"
				);
			}
			else
			{
				jQuery.post
				(
					"/default/index/bookmark2",
					{data:$j(this).attr("rel")},
					function(t)
					{
						if(t.res=="redirect")
							window.location = '/user/login';
						if(a.hasClass('save'))
							a.removeClass('save').addClass('saved').html('<span><?php echo $this->t('Remove Saved Ad')?></span>');							
						else
							a.removeClass('saved').addClass('save').html('<span><?php echo $this->t('Save This Ad')?></span>');		
						
					},
					"json"
				);
			}
			
			return false
		}
	);
	
	<?php if($this->send_message_form->isErrors()):?>
        document.getElementById("send_mid").scrollIntoView();
  	<?php endif;?>
});
<?php $this->JQuery()->javascriptCaptureEnd()?>
<?php
$internal_lng = $this->listing->getInternalLanguage();
	$url_tc = $this->listing->url(false, null, 'tc');
	$url_en = $this->listing->url(false, null, 'en');
?>
<section class="ad_detail">
<!--pre><?php var_dump($this->listing->sold);?></pre-->
	<?php if($this->listing->sold):?>
	<div class="warn sold">
	    <h3><?php echo $this->t('Item has sold or expired.')?></h3>
	    <p><?php echo $this->t('This item has sold or expired. Want to find similar items?')?> <a href="#similar_ad"><?php echo $this->t('See below')?></a></p>
    </div>
    <?php endif;?>
	
	<?php if ($this->listing->at == 1):?>
	<?php if( $this->current_language == 'en'){?>
		<?php if ($internal_lng == 'tc'){?>
			<h1>
				<em><?php echo $this->listing->title?></em>
			</h1>
			<div class="warn">
				<h3><?php echo $this->t('此廣告已被翻譯')?>
							<a class="translate" href="<?php echo $url_en; ?>">
								<span><?php echo $this->t('View Original Ad (English)')?></span>
							</a>
				</h3>
				<p><?php echo $this->t('廣告原文是英語, , 建議您直接向賣家查看內容, 確認所有的細節')?></p>
			</div>
		<?php } else {?>
			<h1>
				<em><?php echo $this->listing->title?></em><a class="translate" href="<?php echo $url_tc?>"><span>View Ad in Chinese</span></a>
			</h1>
		<?php } ?>  
	<?php } else {?>
		<?php if ($internal_lng == 'tc'){?>
			<h1>
				<em><?php echo $this->listing->title?></em>
			</h1>
			<div class="warn">
				<h3><?php echo $this->t('This Ad Has Been Translated')?>
							<a class="translate" href="<?php echo $url_en; ?>">
								<span><?php echo $this->t('View Original Ad (English)')?></span>
							</a>
				</h3>
				<p><?php echo $this->t('This Ad was originally posted in 英文. We recommend reviewing the original Ad and confirming all details with the poster directly.')?></p>
			</div>
		<?php } else {?>
			<h1>
				<em><?php echo $this->listing->title?></em><a class="translate" href="<?php echo $url_tc?>"><span>View Ad in Chinese</span></a>
			</h1>
		<?php }?>  
	<?php }?> 
	<?php endif;?>
	
	<?php if ($this->listing->at == 2):?>
	<?php if( $this->current_language == 'en'){?>
		<?php if ($internal_lng == 'tc'){?>
			<h1>
				<em><?php echo $this->listing->title?></em><a class="translate" href="<?php echo $url_en?>"><span>View Ad in English</span></a>
			</h1>
		<?php } else {?>
			<h1>
				<em><?php echo $this->listing->title?></em>
			</h1>
			<div class="warn">
				<h3>This Ad Has Been Translated
							<a class="translate" href="<?php echo $url_tc; ?>">
								<span>View Original Ad (Chinese)</span>
							</a>
				</h3>
				<p>This Ad was originally posted in Chinese. We recommend reviewing the original Ad and confirming all details with the poster directly.</p>
			</div>
		<?php } ?>  
	<?php } else {?>
		<?php if ($internal_lng == 'tc'){?>
			<h1>
				<em><?php echo $this->listing->title?></em><a class="translate" href="<?php echo $url_en?>"><span>View Ad in English</span></a>
			</h1>
		<?php } else {?>
			<h1>
				<em><?php echo $this->listing->title?></em>
			</h1>
			<div class="warn">
				<h3>This Ad Has Been Translated
							<a class="translate" href="<?php echo $url_tc; ?>">
								<span>View Original Ad (Chinese)</span>
							</a>
				</h3>
				<p>This Ad was originally posted in Chinese. We recommend reviewing the original Ad and confirming all details with the poster directly.</p>
			</div>
		<?php }?>  
	<?php }?> 
	<?php endif;?>

	<?php if (empty($this->listing->at)):?>
		<h1>
			<em><?php echo $this->listing->title?></em>
		</h1>
	<?php endif;?>
	<div class="info">
		<!--Info Section-->
		
		<div class="action info_act">
			<span class="selected"><?php echo $this->t('Info', $internal_lng)?></span>
			<a class="photo_tab"><?php echo $this->t('Photos', $internal_lng)?></a>			
		</div>
		
		<div class="gallery gallery2">
			<a href="#">
				<figure>
					<span><?php if($this->listing->sold):?><span class="sold">(Sold)</span><?php endif;?>
						<?php if(!empty($this->listing->photos)):?>
	                	<img src="<?php echo $this->thumbnail()->scale($this->listing->featured_photo);?>" alt="<?php echo $this->t('__1__ - photo __2__', $internal_lng, $this->listing->title, 1)?>">
	                <?php else:?>
	                    <img src="<?php echo $this->thumbnail()->scale('', 298, 298);?>" alt="<?php echo $this->t('__1__ - photo __2__', $internal_lng, $this->listing->title, 1)?>" id="main_photo">
	                        <?php endif;?>
					</span>
				</figure>
				<p style="color:#2070A0;"><?php echo $this->t('View Photos (__1__)', $internal_lng, $this->listing->phc)?> &raquo;</p>
			</a>
		</div>
		
		<div class="detail general">
                    <dl class="desc">
                        <dt><?php echo $this->t('Posted By', $internal_lng)?>:</dt>                  
                        <?php if($this->listing->user->company  instanceof Model_Company):?>
                            <dd><span><?php echo $this->listing->user->company->company_name?></span><span>(<a href="<?php echo $this->listing->user->company->url()?>"><?php echo $this->t('View All Ads', $internal_lng)?></a>)</span></dd>
                        <?php elseif(!empty($this->listing->user)):?>
                            <dd><span><?php echo $this->listing->user?></span><span>(<a href="<?php echo $this->listing->user->url()?>"><?php echo $this->t('View All Ads', $internal_lng)?></a>)</span></dd>
                        <?php endif; ?>
                    </dl>
                    <?php if(!empty($this->listing->is_free) || !empty($this->listing->is_contact) || !empty($this->listing->price) || !empty($this->listing->sale_price) || !empty($this->listing->rent_price)):?>
                        <?php echo $this->price($this->listing);?>
                    <?php endif;?>
                    <dl>
                        <?php echo $this->dtDd($this->t('Posted', $internal_lng), date(MEDIUM_DATE_FORMAT, $this->listing->posted_at))?>
                    </dl>
                    <dl>
                        <?php echo $this->dtDd($this->t('No. of Views', $internal_lng), $this->listing->number_views)?>
                    </dl>
                </div>
        <?php $partial_name = $this->listing->getAdDetailPartialName();?>
            <?php if(!empty($partial_name)):?>
            <?php echo $this->Partial('mobile/'.$partial_name);?>                
        <?php endif;?>
		<div class="detail description">
			<h2><?php echo $this->t('Description', $internal_lng)?>
			
			<?php if ($this->listing->at == 1):?>
				<?php if( $this->current_language == 'en'){?>
					<?php if ($internal_lng == 'tc'){?>
						<a class="translate" href="<?php echo $url_en; ?>">
							<span class="description_button"><?php echo $this->t('View Original Ad (English)')?></span>
						</a>
					<?php } else {?>
						<a class="translate" href="<?php echo $url_tc?>"><span class="description_button">View Ad in Chinese</span></a>
					<?php } ?>
				<?php } else {?>
					<?php if ($internal_lng == 'tc'){?>
						<a class="translate" href="<?php echo $url_en; ?>">
							<span class="description_button"><?php echo $this->t('View Original Ad (English)')?></span>
						</a>
					<?php } else {?>
						<a class="translate" href="<?php echo $url_tc?>"><span class="description_button">View Ad in Chinese</span></a>
					<?php } ?>
				<?php } ?>
			<?php endif;?>
			
			<?php if ($this->listing->at == 2):?>
				<?php if( $this->current_language == 'en'){?>
					<?php if ($internal_lng == 'tc'){?>
						<a class="translate" href="<?php echo $url_en?>"><span class="description_button">View Ad in English</span></a>
					<?php } else {?>
						<a class="translate" href="<?php echo $url_tc; ?>">
							<span class="description_button">View Original Ad (Chinese)</span>
						</a>
					<?php } ?>
				<?php } else {?>
					<?php if ($internal_lng == 'tc'){?>
						<a class="translate" href="<?php echo $url_en?>"><span class="description_button">View Ad in English</span></a>
					<?php } else {?>
						<a class="translate" href="<?php echo $url_tc?>"><span class="description_button">View Original Ad (Chinese)</span></a>
					<?php } ?>
				<?php } ?>
			<?php endif;?>
			</h2>
                <p>
                    <?php 
                        $text = $this->listing->description;
                        $text = strip_tags($text);
                        $text = preg_replace('#(\+?[0-9][0-9()-\s+]{4,20}[0-9])#u', '<a href="tel:$1">$1</a> ', $text.' ');
                        //$text = preg_replace('#(www.*?)#u', '<a href="http://$1" target="_blank">'.$this->t('Click Here').'</a> ', $text.' ');
                        $text = preg_replace('#((https?://.*?))\s#u', '<a href="$1" target="_blank">'.$this->t('Click Here').'</a>', $text.' ');
                        echo nl2br($text);
                    ?>
					
                </p>
                <?php if(!empty($this->listing->url)):?>
                    <p>
                        <a href="<?php echo $this->listing->url;?>" target="_blank"><?php echo $this->t('Click Here', $internal_lng);?></a>
                    </p>
                <?php endif;?>
                <?php if(!empty($this->listing->user->about_us)):?>
                    <p>
                        <?php //echo $this->listing->user->about_us;?>
                        <?php 
                        $text = $this->listing->user->about_us;
                        $text = strip_tags($text);
                        $text = preg_replace('#(\+?[0-9][0-9()-\s+]{4,20}[0-9])#u', '<a href="tel:$1">$1</a> ', $text.' ');
                        //$text = preg_replace('#(www.*?)#u', '<a href="http://$1" target="_blank">'.$this->t('Click Here').'</a> ', $text.' ');
                        $text = preg_replace('#((https?://.*?))\s#u', '<a href="$1" target="_blank">'.$this->t('Click Here').'</a>', $text.' ');
                        echo nl2br($text);
                    	?>
                    </p>
                <?php endif;?>
		</div>
		
		
		<!--Photo Section-->
		<div class="action photo_act">
			<a class="info_tab"><?php echo $this->t('Info', $internal_lng)?></a>
			<span class="selected"><?php echo $this->t('Photos', $internal_lng)?></span>
		</div>
		<div class="gallery gallery1">
			<figure>
				<span>
					<?php if(!empty($this->listing->photos)):?>
	                	<img src="<?php echo $this->thumbnail()->scale($this->listing->featured_photo);?>" alt="<?php echo $this->t('__1__ - photo __2__', $internal_lng, $this->listing->title, 1)?>" id="main_photo1">
	                <?php else:?>
	                    <img src="<?php echo $this->thumbnail()->scale('', 298, 298);?>" alt="<?php echo $this->t('__1__ - photo __2__', $internal_lng, $this->listing->title, 1)?>" id="main_photo">
	                        <?php endif;?>
				</span>
			</figure>
			<p>
				<?php 
				$i=0;
				$style = 'style="position:absolute; top:50%; left:50%; margin-top:-22px; margin-left:-22px; max-width:45px; max-height:45px;"';
				foreach($this->listing->photos as $k => $photo) :?>
				
					<a><span>
						<img <?php echo ($i==0)? $style:""; $i=2;?> src="<?php echo $this->thumbnail()->resizeAndCrop($photo, 45, 45);?>" class="thumb" alt="<?php echo $this->t('__1__ - photo __2__', null, $this->listing->title, $k)?>" rel2="<?php echo $this->thumbnail()->scale($photo);?>">
					</span></a>
				
	            <?php endforeach;?>
			</p>
		
		</div>
		
		<!--div class="save">
	<?php if (!empty($class)):?>	
                        <p class="action"><a class="<?php echo $class?> to_bookmark" href="#" rel="<?php echo $this->listing->_id?>"><span><?php if ($class == 'save') {echo $this->t('Save');} elseif ($class == 'saved') {echo $this->t('Saved');}?></span></a></p>
	<?php endif; ?>
		</div-->
		<div class="save">
			
                    <?php
                    if(in_array($this->listing->_id, $this->bookmark)) {
                       $class = 'saved';
                    } else {
                       $class = 'save';
                    }
                    ?>
                    <a class="<?php echo $class?> to_bookmark2" href="#" rel="<?php echo $this->listing->_id?>">
                            <span>
                                    <?php if ($class == 'save') {echo $this->t('Save This Ad', $internal_lng);} elseif ($class == 'saved') {echo $this->t('Remove Saved Ad', $internal_lng);}?>
                            </span>
                    </a>
			
		</div>
		<?php if(!$this->listing->sold && !$this->listing->user->no_send_message):?>
			<div class="send_message" id="send_mid">
				<?php if(!empty($this->send_message_form)):?>
		            <?php echo $this->send_message_form->setAttrib('id', 'send_message_form');?>
		        <?php endif;?>		    
			</div>
		<?php endif;?>
	</div>
	<?php if($this->listing->sold):?>
		<a name="similar_ad"></a>
		<?php if(!empty($this->similar_ads_user)):?>
		<section class="pick">
		<?php //var_dump($this->similar_ads_user)?>
		    <h2><?php echo $this->t('Similar Ads from this user');?></h2>
		    <ul>
		    	<?php $i = 1;?>
	          	<?php foreach($this->similar_ads_user as $item):?>
	            <li class="item0<?php echo $i++;?>">
	            	<h1>
		                <a href="<?php echo $item->url[$this->getLanguagePosition()];?>">
		                    <img src="<?php echo $this->thumbnail()->resizeAndCrop($item->photo, 300, 180);?>" alt="<?php echo $item->title?>">
		                    <span><?php echo $item->title?></span>
			            </a>
					</h1>		        	
	            </li>	            
	          	<?php endforeach;?>
		    	<!--li class="item01">
					<h1>
						<a href="http://m.hongkong.asiaclassified.com/tc/Ducati-Monster-M900-4d807c79b7ca870e340f1e6f">
							<img src="20110404_index_files/duc2_300_180_rc.jpg">
							<span>Ducati Monster M900 </span>
						</a>
					</h1>
				</li-->
			</ul>
	    </section>
	    <?php endif;?>
	    <?php if(!empty($this->similar_ads_other)):?>
	    <section class="pick">
	    <?php //var_dump($this->similar_ads_other)?>
		    <h2><?php echo $this->t('Similar Ads from other users');?></h2>
		    <ul>
		    	<?php $i = 1;?>
	          	<?php foreach($this->similar_ads_other as $item):?>
	            <li class="item0<?php echo $i++;?>">
	            	<h1>
		                <a href="<?php echo $item->url[$this->getLanguagePosition()];?>">
		                    <img src="<?php echo $this->thumbnail()->resizeAndCrop($item->photo, 300, 180);?>" alt="<?php echo $item->title?>">
		                    <span><?php echo $item->title?></span>
			            </a>
					</h1>		        	
	            </li>	            
	          	<?php endforeach;?>
			</ul>
	    </section>
	    <?php endif;?>
    <?php endif;?>
    
</section>


<?php echo $this->Partial('mobile/other.phtml', 'default'); 

?>
<?php 
//$user = Model_User::all(array('email' => new MongoRegex('/WakaWaka@ee.com/i')));
//echo "<pre>";print_r($user);echo "</pre>";?>    
